function g = my_funFliter(f,type,m,n,d)
% 参数说明
%   f:图片
%   type:滤波器类型，暂时支持逆谐波均值滤波器，算术均值滤波器amf，
%   几何均值滤波器gmf，中值滤波器mf，修正的阿尔法均值滤波器atmf
%   m，n滤波器核大小
%   d：修正的阿尔法均值滤波器的修正窗口大小
    if nargin == 4
        d = 1;
    end

    % Q = 1.5;
    f = im2double(f);
    [M, N] = size(f);
    %边界扩展

    %填充后的大小
    a = M+m-1;  %长
    b = N+n-1;  %宽

    % 每条边应拓展的长度
    a1 = floor(m/2);
    b1 = floor(n/2);

    gp = zeros(a, b);
    gp(a1+1:a-a1,b1+1:b-b1) = f;  % 索引左右都算在内
    gp(1:a1,:) = gp(m-1:-1:a1+1,:); % 镜像填充
    gp(M+a1+1:M+m-1,:) = gp(M+a1:-1:M+a1-1,:);
    gp(:,1:b1) = gp(:,n-1:-1:b1+1);
    gp(:,N+b1+1:N+n-1) = gp(:,N+b1:-1:N+b1-1);

    switch lower(type)
        case 'ihmf'
            %逆谐波均值滤波
            g = zeros(M, N);
            for x = 2:M+1
                for y = 2:N+1
                    g_m = 0; g_d = 0;
                    for i = -1:1
                        for j = -1:1
                            g_m = g_m + gp(x+i,y+j)^(Q+1);
                            g_d = g_d + gp(x+i,y+j)^Q;
                        end
                    end
                    g(x-1,y-1) = g_m/g_d;
                end
            end
        case 'amf'
            % 算术平均滤波器
            for i = a1+1:M+a1
                for j = b1+1:N+b1
                    s = 0;
                    for k = i-a1:i+a1
                        for l = j-b1:j+b1
                            s = s+gp(k,l);
                        end
                    end
                    g(i-a1,j-b1) = s / (m*n);
                end
            end
        case 'gmf'
            % 几何平均滤波器
            for i = a1+1:M+a1
                for j = b1+1:N+b1
                    s = 1;
                    zeros_nums = 0;
                    for k = i-a1:i+a1
                        for l = j-b1:j+b1
                            if gp(k,l) == 0
                                s = s * 0.05; %如果元素为0，用一个较小的数字来代替
                                %zeros_nums = zeros_nums + 1;
                                continue;
                            else
                                s = s * gp(k,l);
                            end
                        end
                    end
                    g(i-a1,j-b1) = s ^ (1/(m*n));
                    %zeros_nums = 0;
                end
            end
        case 'mf'
            % 中值滤波器
            for i = a1+1:M+a1
                for j = b1+1:N+b1
                    s = [];
                    for k = i-a1:i+a1
                        for l = j-b1:j+b1
                            s(end+1) = gp(k,l);
                        end
                    end
                    g(i-a1,j-b1) = median(s);
                end
            end
        case 'atmf'
            % 阿尔法修正滤波器
            for i = a1+1:M+a1
                for j = b1+1:N+b1
                    s = [];
                    for k = i-a1:i+a1
                        for l = j-b1:j+b1
                            s(end+1) = gp(k,l);
                        end
                    end

                    % 排序后剔除d个元素
                    s = sort(s);
                    s = s(d/2+1:end);
                    s = s(1:end-d/2);

                    g(i-a1,j-b1) = sum(s) / (m*n-d) ;
                end
            end            
    end
end